require(['icheck'], function(icheck){

// icheck 风格
icheck_style = function(){
	$('input').iCheck({
		checkboxClass: 'icheckbox_flat-blue'
	});
}


var query = [];
var linekey = [];//初始化表格行内输入框的key值


checkedIcheck = function(){
    

    $(':checkbox').on('ifChecked',function(){
        $(this).attr('checked', true);
        buildT();
    });

    $(':checkbox').on('ifUnchecked',function(){
        $(this).removeAttr('checked');
        buildT();
    });

    buildT();//如果是编辑页面，去掉注释进行表格初始化


}



function buildT(){
    var specs = [];
    var maxline = 1;//初始化SKU最大行数
    $('#specList').children().each(function(){
        var _checkobj = $(this).find(':checked'), _list = [];
        if(_checkobj.length == 0){
            return;
        }
        $(_checkobj).each(function(index,item){
            _list.push({
                'title':$(item).parent().next().html(),//由于使用了iCheck插件，层级关系发生了变化，需要使用parent()来重新定位lable标签的值
                'id':$(item).val()
            });
        });

        data = {
            'id': $(this).attr('server-id'),
            'title':$(this).attr('server-name'),
            'count':_checkobj.length,
            'childlist':_list
        }
        maxline = maxline*parseInt(_checkobj.length);
        specs.push(data);
    });
    //循环获取rowspan值
    var yu = 1;
    $(specs).each(function(index){
        specs[index].row = (maxline/specs[index]['count'])/yu;
        yu = yu*specs[index].count;
    });
    //构建表格行数据
    var headarr = [], tbodyarr = [];
    $(specs).each(function(index, item){
        headarr.push(item.title);
        tbodyarr = buildTT(tbodyarr, item);
    });
    var headdata = '', tbodydata = '';
    $.each(headarr, function(index){
        headdata += '<th>'+headarr[index]+'</th>';
    });
    headdata += '<th>价格</th><th>库存</th><th>SKU</th>';
    $.each(tbodyarr, function(index){
        tbodydata += '<tr>'
                    +tbodyarr[index]
                    +'<td><input type="text" placeholder="价格" class="input-text" value=""></td>'
                    +'<td><input type="text" placeholder="库存" class="input-text" value=""></td>'
                    +'<td><input type="text" placeholder="SKU" class="input-text" value=""></td>'
                    +'</tr>';
    });
    $('#tableAttr').html('').append('<thead><tr>'+headdata+'</tr></thead>').append('<tbody>'+tbodydata+'</tbody>');
    buildTTT();
}
/**
 * 构建表格每行的td值
 * @param odata 前面经过处理的数据
 * @param adata 需要新添加进去处理的数据
 */
function buildTT(odata, adata){
    var data = [], _linekey = [];
    if(odata.length==0){
        $(adata.childlist).each(function(index, item){
            var _data = '<td rowspan="'+adata.row+'" key="'+item.id+'">'+item.title+'</td>';
            data.push(_data);
            _linekey[index] = item.id;
        });
    }else{
        var i=0;
        $(odata).each(function(index, item){
            $(adata.childlist).each(function(index2, item2){
                var _item = index2 == 0 ? item.concat() : '';
                var _data = _item + '<td rowspan="'+adata.row+'" key="'+item2.id+'">'+item2.title+'</td>';
                data.push(_data);
                _linekey[i] = linekey[index] + '_' +item2.id;
                i++;
            });
        });
    }
    linekey = _linekey;
    return data;
}

function buildTTT(){
    // 获取规格项值字符串
    var itemStr = $('#specItemInfoJson').val();
    // 转换成对象
    var itemObj = JSON.parse( itemStr );

    //对初始化的表格每行的key值进行排序，与入库值保持一致
    $(linekey).each(function(index){
        linekey[index] = linekey[index].split('_').sort().join('_');
    });

    //对初始化的表格input进行赋值
    $('#tableAttr tbody').children('tr').each(function(index){
        var _obj = $(this).find('input');

        var _linekey = linekey[index];

        _obj.eq(0).attr('name', 'price['+_linekey+']');
        _obj.eq(1).attr('name', 'store_count['+_linekey+']');
        _obj.eq(2).attr('name', 'sku['+_linekey+']');

        // 遍历itemObj对象
        jQuery.each(itemObj, function(i, val) {  
            if (val.key == _linekey) {
                _obj.eq(0).val(val.price);
                _obj.eq(1).val(val.store_count);
                _obj.eq(2).val(val.sku);
            }
        });

    });
}



// icheck 风格
icheck_style();

// 点击复选框事件
checkedIcheck();




});

